import QtQuick 2.15
import QtQuick.Controls 2.15

Rectangle {
    id: resizableTextInput
    width: 200
    height: 80
    border.color: "#cccccc"
    border.width: 1
    radius: 4

    // 1. 文本输入框主体
    TextArea {
        id: textField
        anchors {
            fill: parent
            margins: 5
        }
        wrapMode: Text.Wrap
        background: null // 禁用默认背景
    }

    // 2. 右下角拖拽手柄
    Rectangle {
        id: dragHandle
        width: 12
        height: 12
        color: "#21be2b"
        radius: 2
        anchors {
            right: parent.right
            bottom: parent.bottom
            margins: 2
        }

        MouseArea {
            id: resizeArea
            anchors.fill: parent
            cursorShape: Qt.SizeFDiagCursor
            preventStealing: true

            property point startPos: Qt.point(0, 0)

            onPressed: (mouse) => {
                startPos = Qt.point(mouse.x, mouse.y);
            }

            onPositionChanged: (mouse) => {
                // 计算拖拽偏移量
                var deltaX = mouse.x - startPos.x;
                var deltaY = mouse.y - startPos.y;

                // 动态调整父容器尺寸
                resizableTextInput.width += deltaX;
                resizableTextInput.height += deltaY;

                // 限制最小尺寸
                resizableTextInput.width = Math.max(50, resizableTextInput.width);
                resizableTextInput.height = Math.max(30, resizableTextInput.height);
            }
        }
    }

    // 3. 鼠标穿透处理（确保文本框可正常输入）
    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.NoButton
        propagateComposedEvents: true
        onPressed: (mouse) => mouse.accepted = false
        onPositionChanged: (mouse) => mouse.accepted = false
    }
}
